﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;
using System.Data;
using System.Configuration;

namespace KPIS.GERP.GHRMS.DAL
{
    public class TrainingCoursesQuotaDAL : ITrainingCoursesQuotaDAL
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<TrainingCoursesQuotaInfo> GetFull(Nullable<int> train_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<TrainingCoursesQuotaInfo> infoList = new List<TrainingCoursesQuotaInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@train_seq", train_seq);

                strSQL = "SELECT cq.course_quota_seq"
                        + ", js.job_site_name"
                        + ", js.job_site_ref"
                        + ", cq.quota"
                        + ", cq.train_seq"
                        + ", cq.job_site_seq"
                        + ", tc.training_course_descr"
                        + ", wa.work_area_name"
                        + " FROM hrm_training_courses_quota cq"
                        + " Inner Join hrm_training_courses tc ON cq.train_seq = tc.train_seq"
                        + " Inner Join hrm_mp_job_sites js ON cq.job_site_seq = js.job_site_seq"
                        + " Inner Join hrm_mp_work_areas wa ON js.work_area_seq = wa.work_area_seq"
                        + " WHERE cq.record_status not like @delete_status"
                        +(train_seq > 0 ? " AND cq.train_seq = @train_seq " : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        TrainingCoursesQuotaInfo info = new TrainingCoursesQuotaInfo();
                        info.course_quota_seq = Convert.ToInt32(drc[i]["course_quota_seq"]);
                        info.job_site_name = drc[i]["job_site_name"].ToString();
                        info.job_site_seq = Convert.ToInt32(drc[i]["job_site_seq"]);
                        info.job_site_ref = drc[i]["job_site_ref"].ToString();
                        info.train_seq = Convert.ToInt32(drc[i]["train_seq"]);
                        info.training_course_descr = drc[i]["training_course_descr"].ToString();
                        info.quota = Convert.ToInt32(drc[i]["quota"]);
                        info.Area_name = drc[i]["work_area_name"].ToString();
                        
                        infoList.Add(info);

                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public IList<TrainingCoursesQuotaInfo> GetList(Nullable<int> course_quota_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<TrainingCoursesQuotaInfo> infoList = new List<TrainingCoursesQuotaInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@course_quota_seq", course_quota_seq);

                strSQL = "SELECT cq.course_quota_seq"
                        + ", js.job_site_name"
                        + ", cq.quota"
                        + ", cq.train_seq"
                        + ", cq.job_site_seq"
                        + ", tc.training_course_descr"
                        + " FROM hrm_training_courses_quota cq"
                        + " Inner Join hrm_training_courses tc ON cq.train_seq = tc.train_seq"
                        + " Inner Join hrm_mp_job_sites js ON cq.job_site_seq = js.job_site_seq"
                        + " WHERE cq.record_status not like @delete_status"
                        + (course_quota_seq > 0 ? " AND course_quota_seq = @course_quota_seq " : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {

                        TrainingCoursesQuotaInfo info = new TrainingCoursesQuotaInfo();
                        info.course_quota_seq = Convert.ToInt32(drc[i]["course_quota_seq"]);
                        info.job_site_name = drc[i]["job_site_name"].ToString();
                        info.job_site_seq = Convert.ToInt32(drc[i]["scholar_type_descr"]);
                        info.train_seq = Convert.ToInt32(drc[i]["train_seq"]);
                        info.training_course_descr = drc[i]["training_course_descr"].ToString();
                        info.quota = Convert.ToInt32(drc[i]["quota"]);
                        
                        
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public void Insert(TrainingCoursesQuotaInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@job_site_seq", info.job_site_seq);
                dbManager.AddParameters(1, "@train_seq", info.train_seq);
                dbManager.AddParameters(2, "@quota", info.quota);
                dbManager.AddParameters(3, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@created_by", info.created_by);
                dbManager.AddParameters(5, "@created_when", info.created_when);

                strSQL = "INSERT hrm_training_courses_quota"
                    + "(job_site_seq, train_seq"
                    + " , quota, record_status"
                    + ", created_by, created_when)"
                    + " VALUES (@job_site_seq"
                    + ", @train_seq"
                    + ", @quota"
                    + ", @record_status"
                    + ", @created_by"
                    + ", @created_when)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(TrainingCoursesQuotaInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@course_quota_seq", info.course_quota_seq);
                dbManager.AddParameters(1, "@job_site_seq", info.job_site_seq);
                dbManager.AddParameters(2, "@train_seq", info.train_seq);
                dbManager.AddParameters(3, "@quota", info.quota);
                dbManager.AddParameters(4, "@updated_by", info.updated_by);
                dbManager.AddParameters(5, "@updated_when", info.updated_when);

                strSQL = "UPDATE hrm_training_courses_quota"
                    + " SET train_seq = @train_seq"
                    + ", job_site_seq = @job_site_seq"
                    + ", quota = @quota"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE course_quota_seq = @course_quota_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int course_quota_seq, int updated_by)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@course_quota_seq", course_quota_seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", updated_by);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE hrm_training_courses_quota"
                    + " SET record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE course_quota_seq = @course_quota_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
